package controller;

import java.sql.SQLException;

import javax.swing.JOptionPane;

import ui.MessagePopUp;
import ui.Screen;

/** Representing all admin menu related actions, 
 * used to insert parameters in the data package
 * in order to manipulate the data of the table db_user
 * in the database and in the arraylist userlist
 * @author Matthias
 *
 */
public class ATMAdmControl {

	/** ATM object **/
	private final ATM atm;

	/** Constructor of ATMAdmControl
	 * @param atm receiving the object ATM and defining
	 * that the global variable atm is equal to that
	 */
	public ATMAdmControl(final ATM atm) {
		this.atm = atm;
	}

	/**
	 * Method calls BankDatabaseUserSQL and inserts parameters to create a user
	 */
	public final void admCreate() {
		try {
			// getting the input from the textfields
			String userID = atm.getClassScreen().getAdmCreate().getTextField_userID();
			int pin = atm.getClassScreen().getAdmCreate().getPasswordField_PIN();
			String Fname = atm.getClassScreen().getAdmCreate().getTextField_Firstname();
			String Lname = atm.getClassScreen().getAdmCreate().getTextField_Lastname();
			String TelephoneNr = atm.getClassScreen().getAdmCreate().getTextField_Tlf();
			String Streetname = atm.getClassScreen().getAdmCreate().getTextField_Streetname();
			String Streetnr = atm.getClassScreen().getAdmCreate().getTextField_Streetnr();
			String Zipcode = atm.getClassScreen().getAdmCreate().getTextField_Zipcode();
			String City = atm.getClassScreen().getAdmCreate().getTextField_City();

			//checking the input through the regex
			if (checkInput(userID, pin, Fname, Lname, TelephoneNr, Streetname, Streetnr, Zipcode, City) == true) {
				// inserting the user into the database
				atm.getClassBdbUserSQL().insertUserIntoTable(userID, pin, Fname, Lname, TelephoneNr, Streetname,
						Streetnr, Zipcode, City);	
				atm.getClassScreen().getAdmCreate().clearFields();
			} else {}
		} catch (Exception exc) {
			String text = "Wrong Input! Please fill out all fields with the suitable data!";
			new MessagePopUp().setError(text);
		}
	}

	/**
	 * Method calls BankDatabaseUserSQL and inserts parameters to edit a user
	 */
	public final void admEdit() {
		try {
			String userID = atm.getClassScreen().getAdmEdit().getTextField_userID();
			int pin = atm.getClassScreen().getAdmEdit().getPasswordField_PIN();
			String fName = atm.getClassScreen().getAdmEdit().getTextField_Firstname();
			String lName = atm.getClassScreen().getAdmEdit().getTextField_Lastname();
			String telephoneNr = atm.getClassScreen().getAdmEdit().getTextField_Tlf();
			String streetName = atm.getClassScreen().getAdmEdit().getTextField_Streetname();
			String streetNr = atm.getClassScreen().getAdmEdit().getTextField_Streetnr();
			String zipCode = atm.getClassScreen().getAdmEdit().getTextField_Zipcode();
			String city = atm.getClassScreen().getAdmEdit().getTextField_City();
			//checking the input through the regex
			if (checkInput(userID, pin, fName, lName, telephoneNr,
					streetName, streetNr, zipCode, city) == true) {
				// updating the edited user in the database
				atm.getClassBdbUserSQL().editUserFromTable(userID, pin, fName, lName, telephoneNr,
						streetName, streetNr, zipCode, city);
				atm.getClassScreen().show(Screen.ADMEDIT);
			} else {}
		} catch (Exception exc) {
			String text = "Wrong Input! Please fill out all fields with the suitable data!";
			new MessagePopUp().setError(text);
		}
	}

	/**
	 * Method is calling BankDatabaseUserSQL, in order to
	 * delete a user from the database
	 */
	public final void admDelete() {
		try {
			int userID = atm.getClassScreen().getAdmDelete().getSpecificUserID();
			// if user has an account, delete is not allowed
			if (atm.getClassBankDatabase().getUsers(userID).getAccountList().size() > 0) {
				String text = "Delete not possible, since the user"+
						" contains at least one Account";
				new MessagePopUp().setError(text);
			}
			// admin can never be deleted
			else if (userID == 1234) {
				String text = "Admin cannot be deleted!";
				new MessagePopUp().setError(text);
			}
			// getting yes / no input
			else {
				int selectedOption = JOptionPane.showConfirmDialog(
						null, "Are you sure you want to delete the user?", 
								"YES / NO", JOptionPane.YES_NO_OPTION); 
				// if admin selects yes, the user is finally being deleted
				if (selectedOption == JOptionPane.YES_OPTION) {
					// deleting a user from the database
					atm.getClassBdbUserSQL().deleteUserFromTable(userID);
					// refreshing the Jcombobox
					atm.getClassScreen().getAdmDelete().addAllUsers();
				}
			}	
		} catch (SQLException exc) {
			String text = "Could not delete the user!";
			new MessagePopUp().setError(text);
		}
	}

	/** Checking the input of the user through the regex in RegexInput
	 * @param userID id of user
	 * @param pin pin of user
	 * @param fName first name of user
	 * @param lName last name of user
	 * @param telephoneNr telephone number of user
	 * @param streetName street name of user
	 * @param streetNr street number of user
	 * @param zipCode zip code of user
	 * @param city city of user
	 * @return result of the regex check - true / false
	 */
	private boolean checkInput(final String userID, final int pin, 
			final String fName, final String lName,
			final String telephoneNr, final String streetName,
			final String streetNr, final String zipCode, final String city) {
		try {
			// boolean variables for input
			boolean digitsID, digitsPIN, letterFname, letterLname, telDigits,
			letterStreet, sTreetNr, zip, ciTy;
			// checking all the parameters in the Regex
			digitsID = atm.getClassBankDatabase().getClassRegexInput().isEightDigits(userID);
			String pinn = Integer.toString(pin);
			digitsPIN = atm.getClassBankDatabase().getClassRegexInput().isPin(pinn);
			letterFname = atm.getClassBankDatabase().getClassRegexInput().forLetters(fName);
			letterLname = atm.getClassBankDatabase().getClassRegexInput().forLetters(lName);
			telDigits = atm.getClassBankDatabase().getClassRegexInput().isTelNr(telephoneNr);
			letterStreet = atm.getClassBankDatabase().getClassRegexInput().forLetters(streetName);
			sTreetNr = atm.getClassBankDatabase().getClassRegexInput().isStreetNr(streetNr);
			zip = atm.getClassBankDatabase().getClassRegexInput().isZip(zipCode);
			ciTy = atm.getClassBankDatabase().getClassRegexInput().forLetters(city);
			// initializing an array, containing all the results of the regex check
			boolean stuff [] = {digitsID, digitsPIN, letterFname, letterLname, telDigits,
					letterStreet, sTreetNr, zip, ciTy};
			// looping all variables in the array and checking if they 
			// meet the criteria, otherwise return false.
			for (int i = 0; i < stuff.length; i++) {
				if (!stuff[i]) {
					return false;
				}
			}
		} catch (Exception exc) {
			String text = "Wrong input! Please check the input!";
			new MessagePopUp().setError(text);
		}
		return true;
	}

}